package lightswitch.code.connection;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import lightswitch.server.code.gui.MainFrame;
import lightswitch.server.code.sqlhelper.expression.AbstractTable;
import lightswitch.server.code.sqlhelper.expression.NormalTable;
import lightswitch.server.code.sqlhelper.statement.delete.Delete;
import lightswitch.server.code.sqlhelper.statement.insert.Insert;
import lightswitch.server.code.sqlhelper.statement.select.Select;
import lightswitch.server.code.sqlhelper.statement.update.Update;
import lightswitch.server.code.util.StatementType;

public class ClientThread extends Thread {
    private Socket socket;
    private ObjectInputStream br = null;
    private ObjectOutputStream bw = null;

    public ClientThread(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        try {
            do {
                bw = new ObjectOutputStream(socket.getOutputStream());
                br = new ObjectInputStream(socket.getInputStream());
                // listen query string
                String query = ((String) br.readObject()).toLowerCase();
                // parse query.....
                String statementType = StatementType.getStatementType(query);
                Object resultObject = -1;
                try {
                    if (statementType.equals(StatementType.SELECT)) {
                        Select select = new Select(query, null);
                        AbstractTable tb = select.execute();
                        resultObject = AbstractTable.convertToQueryDataTransfering(tb);
                    } else if (statementType.equals(StatementType.INSERT)) {
                        Insert insert = new Insert(query);
                        int eff = insert.execute();
                        if (eff > 0) {
                            // update GUI
                            MainFrame.currentDatabase.flagChangeData = true;
                            MainFrame.initJTable(false, false);
                        }
                        resultObject = eff;
                    } else if (statementType.equals(StatementType.DELETE)) {
                        Delete delete = new Delete(query);
                        int eff = delete.execute();
                        if (eff > 0) {
                            // update GUI
                            MainFrame.currentDatabase.flagChangeData = true;
                            MainFrame.initJTable(false, false);
                        }
                        resultObject = eff;
                    } else if (statementType.equals(StatementType.UPDATE)) {
                        Update update = new Update(query);
                        int eff = update.execute();
                        if (eff > 0) {
                            // update GUI
                            MainFrame.currentDatabase.flagChangeData = true;
                            MainFrame.initJTable(false, false);
                        }
                        resultObject = eff;
                    }
                } catch (Exception exc) {
                    if (statementType.equals(StatementType.SELECT)){
                        resultObject = new QueryDataTransfering(null, null, true);
                    } else {
                        resultObject = -2;
                    }
                }
                bw.writeObject(statementType);
                bw.writeObject(resultObject);
                bw.flush();

            } while (true);
        } catch (ClassNotFoundException cnfe) {
            try {
                bw.close();
                br.close();
                socket.close();
            } catch (IOException ex1) {
            }
        } catch (IOException ex) {
        }
    }
}
